package com.xing.adapter;

import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

import java.util.List;
import com.xing.controller.CommonController;
import com.xing.controller.DBController;
import com.xing.model.HsdModelSurvey;
import com.xing.model.HsdQuestionAndAnswerModelListObj;
import com.xing.my_interface.CallBack;
import com.xing.questionnairesurvey.R;
import com.xing.util.ViewUtil;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ManageListServerAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<HsdModelSurvey> mList;
	private Context mContext;
	private FragmentManager fragmentManager;
	private CallBack myCallBack;
	
	
	public ManageListServerAdapter(Context context,List<HsdModelSurvey> list,FragmentManager frmg,CallBack callBack){
		
		try {
			this.mList = list;
			this.mContext = context;
			this.fragmentManager = frmg;
			this.inflater = LayoutInflater.from(context);
			this.myCallBack = callBack;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("ManageListServerAdapter-------------- Exception");
		}
	}
	

	public int getCount() {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.get(arg0);
		}
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if(mList != null){
			return position;
		}
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final HsdModelSurvey object  = mList.get(position);
		if(convertView == null){
			convertView = inflater.inflate(R.layout.manage_server_list_item, null);
		}
			
		ImageView statusImageView = (ImageView) convertView.findViewById(R.id.status_imgv);
		TextView questionnaireNameTextView = (TextView) convertView.findViewById(R.id.questionnaire_name);
		questionnaireNameTextView.setText(String.valueOf(object.getName()));
		Button button = (Button) convertView.findViewById(R.id.download_btn);
		/**
		 *
		 */
		if (object.getStatus() == 1) {
			statusImageView.setImageResource(R.drawable.not_download_imgv);
		}else if(object.getStatus() == 2){
			statusImageView.setImageResource(R.drawable.has_downloaded_imgv);
		}else if (object.getStatus() == 3) {
//			button.setText("已过期");
		}else if (object.getStatus() == 4) {
//			button.setText("已删除");
		}
		
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (object.getStatus() == 1) {
					 String token = ""; 
					 String username="";
						   
					CommonController.getInstance().getQuestionAndAnswerModelList(object.getSurveyuuid(), username, token, downHandler, mContext);
				}else if (object.getStatus() == 2) {
					ViewUtil.showToast(mContext,"问卷已经下载成功，不可重复下载");
				}
			  
			}
		});
		
		if (position /2 == 0) {
			
		}else {
			
		}
		return convertView;
	}
	
	private Handler downHandler = new Handler(){
		public void handleMessage(Message msg) {
			HsdQuestionAndAnswerModelListObj object = (HsdQuestionAndAnswerModelListObj) msg.obj;
			if (object.status == 1) {
				DBController.getInstance().insertHsdQuestionAndAnswerModelListObj(mContext, object, insertHandler);
			}else{
				ViewUtil.showToast(mContext, object.msg);
			}
		}
	};
	
	private Handler insertHandler = new Handler(){
		public void handleMessage(Message msg) {
			boolean isInsert = (Boolean) msg.obj;
			if (isInsert) {
				myCallBack.fresh();
				ViewUtil.showToast(mContext, "下载成功");
				
			}else{
				ViewUtil.showToast(mContext, "下载失败");
				
			}
				
		}
	};
	
	
	
//	private Handler handler = new Handler(){
//		public void handleMessage(Message msg) {
//			final HsdQuestionAndAnswerModelListObj hsdQuestionModelListObj = (HsdQuestionAndAnswerModelListObj) msg.obj;
//			
//			DBController.getInstance().insertHsdQuestionAndAnswerModelListObj(mContext, hsdQuestionModelListObj, isInsertHandler);
//		}
//	};
//	
//	private Handler isInsertHandler = new Handler(){
//		public void handleMessage(Message msg) {
//			boolean isInsert = (Boolean) msg.obj;
//			ViewUtil.showToast(mContext, "下载服务器问卷成功");
//		}
//	};
}


